package dao.Impl;


import entity.Pet;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import dao.PetDao;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author wfr
 * @date 2019-05-31 10:24
 * @parem PetDaoImpl
 */
public class PetDaoImpl extends BaseDao  implements PetDao {
    public PetDaoImpl() {
        super();
    }
    
    @Override
    public int add(Pet pet) throws SQLException {
        int r=0;
        try {

            PreparedStatement pst =con.prepareStatement("insert into pet(NAME,PRICE,KIND,MASTER_ID,MASTER_KIND) value (?,?,?,?,?)");
            pst.setString(1,pet.getName());
            pst.setInt(2,pet.getPrice());
            pst.setString(3,pet.getKind());
            pst.setInt(4,pet.getMasterId());
            pst.setString(5,pet.getMasterKind());

            r=pst.executeUpdate();
            
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        return r;

    }
    
    @Override
    public Pet getBtId(int id) {
        Pet pet=null;
        try {

            ResultSet resultSet;
            resultSet = con.createStatement().executeQuery("select * from pet where ID="+id);
            if (resultSet.next())
            {
                pet=new Pet();
                pet.setId(resultSet.getInt(1));
                pet.setName(resultSet.getString(2));
                pet.setPrice(resultSet.getInt(3));
                pet.setKind(resultSet.getString(4));
                pet.setMasterId(resultSet.getInt(5));
                pet.setMasterKind(resultSet.getString(6));

            }


        }catch (SQLException e)
        {
            e.printStackTrace();
        }
        return pet;
    }

    @Override
    public List<Pet> getlist(int masterId,String mastertype) {
        List<Pet> pets = new ArrayList<Pet>();
            try {
                String sql = "select * from pet where MASTER_ID=? and MASTER_KIND=?";
               Pet pet = null; 
                PreparedStatement pst = con.prepareStatement(sql);
                // 设置条件(相对于条件查询)
                pst.setInt(1, masterId);
                pst.setString(2, mastertype);
                ResultSet resultSet;
                resultSet = pst.executeQuery();
                
                while (resultSet.next()) {
                    pet = new Pet();
                    pet.setId(resultSet.getInt(1));
                    pet.setName(resultSet.getString(2));
                    pet.setPrice(resultSet.getInt(3));
                    pet.setKind(resultSet.getString(4));
                    pet.setMasterId(resultSet.getInt(5)); 
                    pet.setMasterKind(resultSet.getString(6));
                    pets.add(pet);
                }
              
            } catch (SQLException ex) {
                Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        

        return pets;
    }


    @Override
    public int delete(int id) {
        String sql = "delete from pet where ID=?";

        PreparedStatement pst;
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1,id);
            pst.executeUpdate();
            System.out.println("删除成功");
            return 1;
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return 0;
       

    }

    @Override
    public int updata(int id, String masterType, int masterId) {
        String sql = "update pet set MASTER_ID=? ,MASTER_KIND=? where ID=?";

        PreparedStatement pst;
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1, masterId);
            pst.setString(2, masterType);
            pst.setInt(3, id);
            pst.executeUpdate();
            System.out.println("更新成功");
            return 1;
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return 0;
    }

    @Override
    public List<Pet> getAllPets() {
        List<Pet> pets = new ArrayList<Pet>();
        try {
            String sql = "select * from pet where MASTER_KIND='店主'";
            Pet pet = null;
           
            ResultSet resultSet;
            resultSet = con.createStatement().executeQuery(sql);

            while (resultSet.next()) {
                pet = new Pet();
                pet.setId(resultSet.getInt(1));
                pet.setName(resultSet.getString(2));
                pet.setPrice(resultSet.getInt(3));
                pet.setKind(resultSet.getString(4));
                pet.setMasterId(resultSet.getInt(5));
                pet.setMasterKind(resultSet.getString(6));
                pets.add(pet);
            }

        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return pets;
    }

    @Override
    public Pet getPet(int id, int masterid, String masterkind) {
        Pet pet = null;
        try {

            ResultSet resultSet;
            
            PreparedStatement pst= con.prepareStatement("select * from pet where ID=? and MASTER_ID=? and MASTER_KIND=?");
            pst.setInt(1, id);
            pst.setInt(2, masterid);
            pst.setString(3, masterkind);
            resultSet=pst.executeQuery();
            if (resultSet.next()) {
                pet = new Pet();
                pet.setId(resultSet.getInt(1));
                pet.setName(resultSet.getString(2));
                pet.setPrice(resultSet.getInt(3));
                pet.setKind(resultSet.getString(4));
                pet.setMasterId(resultSet.getInt(5));
                pet.setMasterKind(resultSet.getString(6));

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pet;
    }
}
